package com.zxy.study.ui.activity

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ExtendObserver
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.zxy.study.R
import com.zxy.study.ui.activity.model.MvvmModel

/**
 * @author : Shonn.Zhang
 * @email  : shonn.zhang@mart.blue
 * @time   : 2021/6/25 14:24
 * @desc   :
 */
class MvvmActivity : AppCompatActivity() {

    private lateinit var mvvmModel: MvvmModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mvvm)

        mvvmModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application)).get(MvvmModel::class.java)
        mvvmModel.data.observe(this, Observer<String> { t -> Log.d("AAA", t) })

//        mvvmModel.data.observe(this,ExtendObserver<String>(mvvmModel.data, Observer {
//            Log.d("AAA",it)
//        }))
    }

    fun onClick(v: View) {
        Log.d("AAA","3s后发送状态变更")

        Handler(Looper.getMainLooper()).postDelayed({
            mvvmModel.data.value = "1000"
        },3000)
    }

    fun gotoMVVM(v : View){
//        mvvmModel.data.removeObservers(this)
        startActivity(Intent(this,MVVMDemoActivity::class.java))
    }

}